package com.example.peibei.ui.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.dingtao.common.bean.Dynamic;
import com.dingtao.common.bean.Order;
import com.dingtao.common.bean.RefreshResult;
import com.dingtao.common.bean.Search;
import com.dingtao.common.bean.SearchResult;
import com.dingtao.common.core.DataCall;
import com.dingtao.common.core.WDActivity;
import com.dingtao.common.core.exception.ApiException;
import com.dingtao.common.util.SPUtils;
import com.example.peibei.R;
import com.example.peibei.base.SpConstant;
import com.example.peibei.service.presenter.DashDataPresenter;
import com.example.peibei.service.presenter.FollowPresenter;
import com.example.peibei.service.presenter.SearchPresenter;
import com.example.peibei.ui.adapter.RecommendAdapter;
import com.example.peibei.ui.adapter.SearchAdapter;
import com.example.peibei.ui.fragment.DynamicListFragment;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class SearchActivity extends WDActivity {
    @BindView(R.id.et_search)
    EditText et_search;
    @BindView(R.id.tv_cancel)
    TextView tv_cancel;
    @BindView(R.id.rv_search_list)
    RecyclerView rv_search_list;
    @BindView(R.id.rv_hot_list)
    RecyclerView rv_hot_list;

    private SearchPresenter searchPresenter;
    private List<SearchResult.DataDTO.ResultDTO> searchList;
    private List<SearchResult.DataDTO.ResultDTO> recommendList;
    private SearchAdapter searchAdapter;
    private RecommendAdapter recommendAdapter;
    private SPUtils spUtils;
    private String token;
    private int updatePositionResult;
    private int updatePositionRecommend;
    private FollowPresenter followPresenter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_search;
    }

    @Override
    protected void initView() {
        spUtils = new SPUtils(SearchActivity.this, SpConstant.USER_TOKEN);
        token = spUtils.getString(SpConstant.USER_TOKEN);
        searchList = new ArrayList<>();
        recommendList = new ArrayList<>();
        searchPresenter = new SearchPresenter(new SearchCall());
        searchAdapter = new SearchAdapter(searchList);
        rv_search_list.setLayoutManager(new LinearLayoutManager(this));
        rv_search_list.setAdapter(searchAdapter);

        recommendAdapter = new RecommendAdapter(recommendList);
        rv_hot_list.setLayoutManager(new LinearLayoutManager(this));
        rv_hot_list.setAdapter(recommendAdapter);
        searchPresenter.reqeustList("推荐", "");
        followPresenter = new FollowPresenter(new FollowCall());
        et_search.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String trim = s.toString().trim();
                if (!trim.equals("")) {
                    searchPresenter.reqeustList(trim, "");
                }
            }
        });
        searchAdapter.addChildClickViewIds(R.id.ll_no_follow, R.id.ll_followed);
        searchAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            if (view.getId() == R.id.ll_no_follow || view.getId() == R.id.ll_followed) {
                updatePositionRecommend = position;
                SearchResult.DataDTO.ResultDTO item = (SearchResult.DataDTO.ResultDTO) adapter.getData().get(position);
                int follow = item.isFollow() ? 0 : 1;
                long id = item.getId();
//                followPresenter.reqeust(follow, "Bearer " + token, id + "");
                followPresenter.reqeust("Bearer " + token, id + "", follow + "");
            }
        });
        recommendAdapter.addChildClickViewIds(R.id.ll_no_follow, R.id.ll_followed);
        recommendAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            if (view.getId() == R.id.ll_no_follow || view.getId() == R.id.ll_followed) {
                updatePositionRecommend = position;
                SearchResult.DataDTO.ResultDTO item = (SearchResult.DataDTO.ResultDTO) adapter.getData().get(position);
                int follow = item.isFollow() ? 0 : 1;
                long id = item.getId();
                followPresenter.reqeust(follow, "Bearer " + token, id + "");
            }
        });
        searchAdapter.setOnItemClickListener((adapter, view, position) -> {
            SearchResult.DataDTO.ResultDTO result = (SearchResult.DataDTO.ResultDTO) adapter.getData().get(position);
            Bundle bundle = new Bundle();
            bundle.putString("mid",result.getId()+"");
            intent(PersonInfoActivityTwo.class,bundle);
        });
        recommendAdapter.setOnItemClickListener((adapter, view, position) -> {
            SearchResult.DataDTO.ResultDTO result = (SearchResult.DataDTO.ResultDTO) adapter.getData().get(position);
            Bundle bundle = new Bundle();
            bundle.putString("mid",result.getId()+"");
            intent(PersonInfoActivityTwo.class,bundle);
        });
        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String trim = et_search.getText().toString().trim();
                if (!trim.equals("")) {
                    searchPresenter.reqeustList(trim, "");
                }
            }
        });

    }

    class SearchCall implements DataCall<SearchResult> {

        @Override
        public void success(SearchResult data, Object... args) {
            searchAdapter.setNewInstance(data.getData().getResult());
            recommendAdapter.setNewInstance(data.getData().getRecommend());
        }

        @Override
        public void fail(ApiException e, Object... args) {

        }
    }

    class FollowCall implements DataCall<Dynamic> {

        @Override
        public void success(Dynamic data, Object... args) {
            //还需要刷新处理
            SearchResult.DataDTO.ResultDTO resultDTO = recommendAdapter.getData().get(updatePositionRecommend);
            resultDTO.setFollow(!resultDTO.isFollow());
            recommendAdapter.notifyItemChanged(updatePositionRecommend);
        }

        @Override
        public void fail(ApiException e, Object... args) {
            EventBus.getDefault().post(RefreshResult.getInstance(true, true));
        }
    }

    @OnClick(R.id.iv_back)
    public void back() {
        finish();
    }

    @Override
    protected void destoryData() {

    }
}
